package com.screwdriver.jso.client.converter;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArrayString;

import java.math.BigDecimal;

/**
 * Created by IntelliJ IDEA.
 * User: jan kowalski
 * Date: 27.08.11
 * Time: 23:31
 * To change this template use File | Settings | File Templates.
 */
public class BigDecimalConverter {

    public static String toJsoValue(BigDecimal e) {
        return e.toString();
    }

    public static BigDecimal toJavaValue(String value) {
        if (value != null)
            return new BigDecimal(value);
        else
            return null;
    }

    public static JsArrayString toJsoValue(BigDecimal[] e) {
        if (e == null)
            return null;

        JsArrayString array = (JsArrayString) JavaScriptObject.createArray();
        for (BigDecimal b : e) {
            array.push(toJsoValue(b));
        }

        return array;
    }

    public static BigDecimal[] toJavaValue(JsArrayString jsArrayString) {
        if (jsArrayString == null)
            return null;
        BigDecimal[] bigDecimals = new BigDecimal[jsArrayString.length()];
        for (int i = 0; i < jsArrayString.length(); i++) {
            bigDecimals[i] = toJavaValue(jsArrayString.get(i));
        }
        return bigDecimals;
    }


}
